import {ToPath} from "@/api/mixins/ToPath";
import {UserModel} from "@/api/model/user/UserModel";
import {AxiosPromise} from "axios";
import {BaseService, Request, Result, Service} from "csap-typescript-vue2/lib";

@Service()
export class UserService extends BaseService<UserModel, UserModel, UserModel, number> {

    /**
     * 登录
     */
    login(data: Request<UserModel>): AxiosPromise<Result<UserModel>> {
        return this.getHttpRequest().execute<UserModel, Result<UserModel>>(data);
    }

    /**
     * 退出登录
     */
    loginOut(data: Request<UserModel>) {
        let ret = this.getHttpRequest().execute<UserModel, Result<boolean>>(data)
        ret.then(response => {
            localStorage.removeItem("user");
            ToPath.toPath("/login", {})
            return response;
        })
        return ret;
    }

    /**
     * 锁定
     * @param id
     * @param baseFrameWorkParam
     * @returns {AxiosPromise}
     */
    lock(id: Request<{ id: number }>): AxiosPromise<object> {
        return this.getHttpRequest().get(id);
    }

    /**
     *
     * @param id
     * @param baseFrameWorkParam
     * @returns {AxiosPromise}
     */
    unLock(id: Request<{ id: number }>): AxiosPromise<object> {
        return this.getHttpRequest().get(id);
    }

    /**
     * 修改用户
     * @param data
     * @param baseFrameWorkParam
     * @returns {AxiosPromise}
     */
    updateByRuleUser(data: Request<UserModel>): AxiosPromise<object> {
        return this.getHttpRequest().put(data);
    }

    /**
     * 根据用户ID 修改密码
     * @param id
     * @param password
     * @param baseFrameWorkParam
     * @returns {AxiosPromise}
     */
    updateByUserPassword(data: Request<{ id: number, password: string }>): AxiosPromise<object> {
        return this.getHttpRequest().put(data);
    }

    /**
     * 修改个人密码
     * @param id
     * @param password
     * @param baseFrameWorkParam
     * @returns {any}
     */
    updatePassword(data: Request<{ password: string, newPassword: string }>): AxiosPromise<object> {
        return this.getHttpRequest().put(data);
    }
}
